﻿Imports System
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Workflow.ComponentModel

Namespace System.Workflow.ComponentModel.Design
    Friend Class DynamicAction
        Implements IDisposable
        ' Methods
        Friend Sub New()
            Me.buttons = New ItemList([Of] ActionButton)(Me)
        End Sub

        Public Sub Dispose()
            Me.Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub

        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            Dim button As ActionButton
            For Each button In Me.buttons
                DirectCast(button, IDisposable).Dispose
            Next
            Me.buttons.Clear
        End Sub

        Friend Sub Draw(ByVal graphics As Graphics)
            If ((Me.transparency <> 0!) AndAlso (Me.buttons.Count <> 0)) Then
                ActivityDesignerPaint.Draw3DButton(graphics, Nothing, Me.Bounds, (Me.transparency - 0.1!), ButtonState.Normal)
                Dim i As Integer
                For i = 0 To Me.buttons.Count - 1
                    Dim buttonBounds As Rectangle = Me.GetButtonBounds(i)
                    Dim button As ActionButton = Me.buttons.Item(i)
                    If (button.StateImages.Length = 1) Then
                        Dim image As Image = button.StateImages(0)
                        If ((button.State = States.Normal) OrElse (button.State = States.Disabled)) Then
                            buttonBounds.Inflate(-2, -2)
                            ActivityDesignerPaint.DrawImage(graphics, image, buttonBounds, New Rectangle(Point.Empty, image.Size), DesignerContentAlignment.Fill, Me.transparency, (button.State = States.Disabled))
                        Else
                            Dim buttonState As ButtonState = If((button.State = States.Highlight), ButtonState.Normal, ButtonState.Pushed)
                            ActivityDesignerPaint.Draw3DButton(graphics, image, buttonBounds, Me.transparency, buttonState)
                        End If
                    Else
                        Dim image2 As Image = Me.buttons.Item(i).StateImages(CInt(Me.buttons.Item(i).State))
                        buttonBounds.Inflate(-2, -2)
                        ActivityDesignerPaint.DrawImage(graphics, image2, buttonBounds, New Rectangle(Point.Empty, image2.Size), DesignerContentAlignment.Fill, Me.transparency, False)
                    End If
                Next i
            End If
        End Sub

        Protected Overrides Sub Finalize()
            Try 
                Me.Dispose(False)
            Finally
                MyBase.Finalize
            End Try
        End Sub

        Friend Function GetButtonBounds(ByVal buttonIndex As Integer) As Rectangle
            If ((buttonIndex < 0) OrElse (buttonIndex >= Me.buttons.Count)) Then
                Throw New ArgumentOutOfRangeException("buttonIndex")
            End If
            Dim empty As Rectangle = Rectangle.Empty
            empty.X = ((Me.borderSize.Width + (buttonIndex * Me.buttonSize.Width)) + ((buttonIndex + 1) * Me.margin.Width))
            empty.Y = (Me.borderSize.Height + Me.margin.Height)
            empty.Size = Me.buttonSize
            Return empty
        End Function


        ' Properties
        Friend ReadOnly Property ActionType As ActionTypes
            Get
                Return Me.actionType
            End Get
        End Property

        Friend ReadOnly Property Bounds As Rectangle
            Get
                Dim empty As Size = Size.Empty
                Dim num As Integer = Math.Max(1, Me.buttons.Count)
                empty.Width = (((2 * Me.borderSize.Width) + (num * Me.buttonSize.Width)) + ((num + 1) * Me.margin.Width))
                empty.Height = (((2 * Me.borderSize.Height) + Me.buttonSize.Height) + (2 * Me.margin.Height))
                Return New Rectangle(Point.Empty, empty)
            End Get
        End Property

        Friend ReadOnly Property Buttons As IList([Of] ActionButton)
            Get
                Return Me.buttons
            End Get
        End Property

        Friend Property ButtonSize As ButtonSizes
            Get
                Return Me.buttonSizeType
            End Get
            Set(ByVal value As ButtonSizes)
                If (Me.buttonSizeType <> value) Then
                    Me.buttonSizeType = value
                    Me.buttonSize = DynamicAction.Sizes(CInt(Me.buttonSizeType))
                    Me.margin = DynamicAction.Margins(CInt(Me.buttonSizeType))
                End If
            End Set
        End Property

        Friend Property DockAlignment As DesignerContentAlignment
            Get
                Return Me.dockAlignment
            End Get
            Set(ByVal value As DesignerContentAlignment)
                If (Me.dockAlignment <> value) Then
                    Me.dockAlignment = value
                End If
            End Set
        End Property

        Friend Property DockMargin As Size
            Get
                Return Me.dockMargin
            End Get
            Set(ByVal value As Size)
                Me.dockMargin = value
            End Set
        End Property

        Friend Property Transparency As Single
            Get
                Return Me.transparency
            End Get
            Set(ByVal value As Single)
                If (Me.transparency <> value) Then
                    Me.transparency = Math.Max(DynamicAction.DefaultTransparency, value)
                End If
            End Set
        End Property


        ' Fields
        Private actionType As ActionTypes = ActionTypes.Standard
        Private borderSize As Size = New Size(2, 2)
        Private buttons As ItemList([Of] ActionButton)
        Private buttonSize As Size = DynamicAction.Sizes(2)
        Private buttonSizeType As ButtonSizes = ButtonSizes.Medium
        Private Shared DefaultTransparency As Single = 0!
        Private dockAlignment As DesignerContentAlignment = DesignerContentAlignment.TopLeft
        Private dockMargin As Size = DynamicAction.Sizes(2)
        Private margin As Size = DynamicAction.Margins(2)
        Private Shared Margins As Size() = New Size() { New Size(1, 1), New Size(1, 1), New Size(2, 2), New Size(2, 2), New Size(3, 3) }
        Private minimumTransparency As Single = DynamicAction.DefaultTransparency
        Private Shared Sizes As Size() = New Size() { New Size(20, 20), New Size(&H18, &H18), New Size(&H1C, &H1C), New Size(&H20, &H20), New Size(&H24, &H24) }
        Private transparency As Single = DynamicAction.DefaultTransparency

        ' Nested Types
        Friend Enum ActionTypes
            ' Fields
            Standard = 1
            TwoState = 2
        End Enum

        Friend Enum ButtonSizes
            ' Fields
            Large = 4
            Medium = 2
            MediumLarge = 3
            Small = 0
            SmallMedium = 1
        End Enum
    End Class
End Namespace

